/*
 * MinaMainServerHandler.java
 *
 * Copyright 2012 the original author or authors(ninglong).
 *
 * Licensed under the testMina, Version 1.0 (the "License");
 *
 */
package org.geek.demo.mina.server;

import java.util.Scanner;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

/**   
 * <p>服务器端消息处理</p>
 * @author ninglong bhd2973@qq.com   
 * @date May 2, 2012 9:58:48 PM
 * @version V1.0   
 */
public class Server extends IoHandlerAdapter{

	/**
	 *当一个客户端连接进入时
	 */
	@Override
	public void sessionOpened(IoSession iosession) throws Exception {
		System.out.println("客户IP:"+iosession.getRemoteAddress());
	}

	/**
	 *当连接关闭时
	 */
	@Override
	public void sessionClosed(IoSession iosession) throws Exception {
		System.out.println("客户"+iosession.getRemoteAddress()+"断开连接了");
	}

	/**
	 *接收消息
	 */
	@Override
	public void messageReceived(IoSession iosession, Object obj)
			throws Exception {
		String str = (String)obj;
		System.out.println("客户端:"+str);
		Scanner sc = new Scanner(System.in);
		String msg = sc.nextLine();
		iosession.write(msg);
	}
}
